package com.mju.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.mju.exception.ServiceException;
import com.mju.model.Student;
import com.mju.service.StudentService;
import com.mju.utils.RespData;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@RestController
@RequestMapping("/student")
public class StudentController {
	
	@Autowired
	private StudentService studentService;
	
	/*
	 * @RequestMapping("/list") public RespData list(Student student,
	 * 
	 * @RequestParam(name="pageNo",defaultValue = "1")Integer pageNo,
	 * 
	 * @RequestParam(name="pageSize",defaultValue = "15")Integer pageSize) { return
	 * studentService.getList(student, pageNo, pageSize); }
	 */
	
	@GetMapping("/list")
	public RespData list(Student student,
			Integer  pageNo,Integer pageSize) {
		Page<Student> page=studentService.getList(student, pageNo, pageSize);
		return  RespData.data(page);
	}
	
	@PostMapping("/add")
	public RespData add(@RequestBody Student student) throws ServiceException {
		studentService.addStudent(student);
		return RespData.success("添加成功!");
	}
	
	@GetMapping("/delete")
	public RespData delete(String studentno) throws ServiceException {
		studentService.deleteStudent(studentno);
		return RespData.success("删除成功!");
	}

	@PostMapping("/edit")
	public RespData edit(@RequestBody Student student) throws ServiceException {
		studentService.editStudent(student);
		return RespData.success("修改成功!");
	}
		
	@GetMapping("/{studentno}")
	public RespData test(@PathVariable String studentno) throws ServiceException {
		Student student=studentService.getStudentById(studentno);
		if(student==null)
			return RespData.failed("无该学号学生",1001);
		Map<String,Object> map=new HashMap<>();
		map.put("studentno", student.getStudentno());
		map.put("studentname",student.getStudentname());
		return RespData.data(map);
	}	
}
